package edu.fdzc.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.fdzc.system.common.Constants;
import edu.fdzc.system.common.enums.ResultCodeEnum;
import edu.fdzc.system.common.enums.RoleEnum;
import edu.fdzc.system.entity.Accout;
import edu.fdzc.system.entity.Admin;
import edu.fdzc.system.exception.CustomException;
import edu.fdzc.system.mapper.AdminMapper;
import edu.fdzc.system.service.AdminService;
import edu.fdzc.system.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void add(Admin admin) {
        Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
//        System.out.println(admin);
        log.info("adming:{}", admin);
        if (ObjectUtil.isNotNull(dbAdmin)) {
            log.info("adming1:{}", admin);
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(admin.getPassword())) {
            log.info("adming2:{}", admin);
            admin.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(admin.getName())) {
            log.info("adming3:{}", admin);
            admin.setName(admin.getUsername());
        }
        admin.setRole(RoleEnum.ADMIN.name());
        log.info("adming4:{}", admin);
        adminMapper.insert(admin);
    }

    @Override
    public Admin selectById(Integer integer) {
        return adminMapper.selectById(integer);
    }

    @Override
    public void updateById(Admin admin) {
        adminMapper.updateById(admin);
    }

    @Override
    public List<Admin> selectAll(Admin admin) {
        return adminMapper.selectAll(admin);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        for(Integer id:ids){
            adminMapper.deleteById(id);
        }
    }

    @Override
    public void delectById(Integer id) {
//        return null;
        adminMapper.deleteById(id);
    }

    @Override
    public PageInfo<Admin> selectPage(Admin admin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> list = adminMapper.selectAll(admin);
        return PageInfo.of(list);
    }

    @Override
    public Accout login(Accout account) {
        Admin dbAdmin = adminMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!dbAdmin.getPassword().equals(account.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String token = TokenUtils.createToken(dbAdmin.getId() + "-" + dbAdmin.getRole(), dbAdmin.getPassword());
        dbAdmin.setToken(token);
        log.info("adming:{}", dbAdmin);
        System.out.println(dbAdmin);
        return dbAdmin;

    }

    @Override
    public void updatePassword(Accout account) {
        Admin dbAdmin = adminMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbAdmin.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        dbAdmin.setPassword(account.getNewPassword());
        adminMapper.updateById(dbAdmin);
    }
}
